package br.com.hsp.system.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.hsp.organizacao.Consulta;


public class ConsultaRepository {
	
	private EntityManager manager ;
	
	public ConsultaRepository ( EntityManager manager ) {
		this.manager = manager ;
	}
	
	public void add(Consulta consulta) {
		this.manager.persist(consulta);
	}
	
	public void remove(Long id) {
		Consulta consulta = this.procura(id);
		this.manager.remove(consulta);
	}
	
	public Consulta atualiza(Consulta consulta) {
		return this.manager.merge(consulta);
	}
	
	public Consulta procura(Long id) {
		return this.manager.find(Consulta.class, id);
	}
	
	public List<Consulta> getLista() {
		Query query = this.manager.createQuery("SELECT x from Consulta x");
		return query.getResultList();
	}
	
	public List<Consulta> getConsultasMedC(Long medId) {
		Query query = this.manager.createQuery("SELECT x from Consulta x where medico_id="+ medId);
		return query.getResultList();
	}
	
	public List<Consulta> getConsultasPaciente(Long pacienteId) {
		Query query = this.manager.createQuery("SELECT x from Consulta x where paciente_id="+ pacienteId);
		return query.getResultList();
	}
	
}
